﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace EventScavenger
{
    public partial class MachineLogStatusses : Form
    {
        public EventScavengerDAL EventScavengerDAL;

        public MachineLogStatusses()
        {
            InitializeComponent();
        }

        private ListViewGroup GetListViewGroupByName(string name)
        {
            ListViewGroup group = null;
            foreach (ListViewGroup lvg in lvwEventMachineLogs.Groups)
            {
                if (lvg.Name.ToUpper() == name.ToUpper())
                {
                    group = lvg;
                    break;
                }
            }
            if (group == null)
            {
                group = new ListViewGroup(name.ToUpper(), name.ToUpper());
                lvwEventMachineLogs.Groups.Add(group);
            }
            return group;
        }
        private void RefreshList()
        {
            ListViewItem lvwi;
            MachineLogEntry mleSelected = null;
            List<MachineLogEntry> machineLogList;
            int totalCount = 0;
            if (lvwEventMachineLogs.SelectedItems.Count > 0)
            {
                mleSelected = (MachineLogEntry)lvwEventMachineLogs.SelectedItems[0].Tag;
            }
            Cursor.Current = Cursors.WaitCursor;
            lvwEventMachineLogs.Items.Clear();
            try
            {
                lvwEventMachineLogs.BeginUpdate();
                Cursor.Current = Cursors.WaitCursor;
                machineLogList = EventScavengerDAL.GetMachineLogList(!showDisabledToolStripMenuItem.Checked);
                if (nameToolStripMenuItem1.Checked)
                    machineLogList = (from me in machineLogList
                                      orderby me.Machine + "\\" + me.LogName
                                      select me).ToList();
                else if (lastUpdateDateToolStripMenuItem1.Checked)
                    machineLogList = (from me in machineLogList
                                      orderby me.LastPollTime descending
                                      select me).ToList();
                else if (oldestEntryToolStripMenuItem1.Checked)
                    machineLogList = (from me in machineLogList
                                      orderby me.OldestEntry
                                      select me).ToList();
                else if (newestEntryToolStripMenuItem1.Checked)
                    machineLogList = (from me in machineLogList
                                      orderby me.NewestEntry descending
                                      select me).ToList();
                else // if (entryCountToolStripMenuItem.Checked)
                    machineLogList = (from me in machineLogList
                                      orderby me.EntryCount descending
                                      select me).ToList();
                List<ListViewItem> lvitems = new List<ListViewItem>();
                foreach (MachineLogEntry mle in (from ml in machineLogList
                                                 orderby ml.Collector
                                                 select ml))
                {
                    lvwi = new ListViewItem(mle.Machine + "\\" + mle.LogName);
                    lvwi.Group = GetListViewGroupByName(mle.Collector);
                    if (!mle.Enabled && !mle.DBImportEnabled)
                    {
                        lvwi.ForeColor = Color.DarkGray;
                    }
                    lvwi.SubItems.Add(mle.LastPollTime.ToString("yyyy-MM-dd HH:mm"));
                    lvwi.SubItems.Add(mle.PollFrequency.ToString());
                    lvwi.SubItems.Add(mle.OldestEntry.ToString("yyyy-MM-dd HH:mm"));
                    lvwi.SubItems.Add(mle.NewestEntry.ToString("yyyy-MM-dd HH:mm"));
                    lvwi.SubItems.Add(mle.EntryCount.ToString());
                    totalCount += mle.EntryCount;
                    lvwi.Tag = mle;
                    lvitems.Add(lvwi);


                    if ((mleSelected != null) && (mleSelected.MachineLogID == mle.MachineLogID))
                    {
                        lvwi.Selected = true;
                    }
                }
                lvwEventMachineLogs.Items.AddRange(lvitems.ToArray());
                toolStripStatusLabelLogCount.Text = lvwEventMachineLogs.Items.Count.ToString() + " log(s)";
                toolStripStatusLabelRecords.Text = totalCount.ToString() + " record(s)";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString() + "\r\n" + EventScavengerDAL.LastError, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                lvwEventMachineLogs.EndUpdate();
                Cursor.Current = Cursors.Default;
                if (lvwEventMachineLogs.SelectedItems.Count > 0)
                    lvwEventMachineLogs.SelectedItems[0].EnsureVisible();
            }
        }

        private void MachineLogStatusses_Shown(object sender, EventArgs e)
        {
            RefreshList();
        }

        private void refreshToolStripMenuItem_Click(object sender, EventArgs e)
        {
            RefreshList();
        }

        private void toolStripButton5_Click(object sender, EventArgs e)
        {
            RefreshList();
        }
        private void SetSort(bool name, bool lastUpdate, bool oldest, bool newest, bool count)
        {

            nameToolStripMenuItem1.Checked = name;
            lastUpdateDateToolStripMenuItem1.Checked = lastUpdate;
            oldestEntryToolStripMenuItem1.Checked = oldest;
            newestEntryToolStripMenuItem1.Checked = newest;
            entryCountToolStripMenuItem1.Checked = count;
            RefreshList();
        }
        private void nameToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SetSort(true, false, false, false, false);
        }
        private void lastUpdateDateToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SetSort(false, true, false, false, false);
        }
        private void oldestEntryToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SetSort(false, false, true, false, false);
        }
        private void newestEntryToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SetSort(false, false, false, true, false);
        }
        private void entryCountToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SetSort(false, false, false, false, true);
        }

        private void showDisabledToolStripMenuItem_Click(object sender, EventArgs e)
        {
            toolStripButtonShowDisabledEntries.Checked = showDisabledToolStripMenuItem.Checked;
            RefreshList();
        }
        private void toolStripButtonShowDisabledEntries_Click(object sender, EventArgs e)
        {
            showDisabledToolStripMenuItem.Checked = toolStripButtonShowDisabledEntries.Checked;
            RefreshList();
        }
    }
}
